package com.gql.lambda;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * description: Lambda表达式语法
 * @author: gql
 * @date 2022/04
 */
public class Lambda02Test {

    @Test
    public void lambdaTest() {
        // 1. 无参,无返回值
        Runnable r1 = () -> {
            System.out.println("Hello Lambda!");
        };
        r1.run();

        // 2. 1个参数,无返回值
        Consumer<String> con1 = (String str) -> {
            System.out.println(str);
        };
        con1.accept("Hello Lambda!!");


        // 3. 省略数据类型,自动类型推断
        Consumer<String> con2 = (str) -> {
            System.out.println(str);
        };
        con2.accept("Hello Lambda!!!");

        // 4.省略参数小括号
        Consumer<String> con3 = str -> {
            System.out.println(str);
        };
        con3.accept("Hello Lambda!!!!");

        // 5.两个以上参数,多条执行语句,有返回值
        Comparator<Integer> com1 = (x, y) -> {
            return Integer.compare(x, y);
        };
        System.out.println(com1.compare(1, 2));


        // 6.Lambda体只有一条语句时,return与大括号都有,都可省略
        Comparator<Integer> com2 = (x, y) -> Integer.compare(x, y);
        System.out.println(com2.compare(1, 2));
    }
}
